package com.businesscard.system.config;

import com.businesscard.system.entity.Admin;
import com.businesscard.system.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 数据初始化配置
 */
@Configuration
public class DataInitConfig {

    @Autowired
    private AdminMapper adminMapper;
    
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    
    @Bean
    public CommandLineRunner initData() {
        return args -> {
            // 检查是否已存在管理员账户
            if (adminMapper.selectByUsername("admin") == null) {
                // 创建默认管理员账户
                Admin admin = new Admin();
                admin.setUsername("admin");
                admin.setPassword(passwordEncoder.encode("admin123"));
                admin.setEmail("admin@example.com");
                
                adminMapper.insert(admin);
                
                System.out.println("初始化管理员账户成功，用户名：admin，密码：admin123");
            }
        };
    }
} 